﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class LabelLine
    Inherits PowerPacks.SimpleShape

    Private M_PStart As Point
    Private M_PStop As Point
    Private m_BodyColor As Color = Color.DarkGray
    Private m_IdentColor As Color = Color.LightGray
    Private m_BodyWidth As Integer = 8
    Private m_IdentWidth As Integer = 2
    Private m_LineStyle As LineStyleConstants = LineStyleConstants.Line
    Public Enum LineStyleConstants
        Solid = 0
        Line = 1
        Circle = 2
        Square = 3
        Hatch = 4
    End Enum
    Public Sub New()
        '(ControlStyles.OptimizedDoubleBuffer, True)
        'Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        ' Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        ' Me.SetStyle(ControlStyles.UserPaint, True)
        'Me.UpdateStyles()
        Me.BackColor = Color.Transparent
        Me.Width = 1
        Me.Height = 1
    End Sub
    Public Property X1() As Integer
        Get
            Return M_PStart.X
        End Get
        Set(ByVal value As Integer)
            M_PStart.X = value
        End Set
    End Property
    Public Property X2() As Integer
        Get
            Return M_PStop.X
        End Get
        Set(ByVal value As Integer)
            M_PStop.X = value
        End Set
    End Property
    Public Property Y1() As Integer
        Get
            Return M_PStart.Y
        End Get
        Set(ByVal value As Integer)
            M_PStart.Y = value
        End Set
    End Property
    Public Property Y2() As Integer
        Get
            Return M_PStop.Y
        End Get
        Set(ByVal value As Integer)
            M_PStop.Y = value
        End Set
    End Property
    Public Property StartPoint() As Point
        Get
            Return M_PStart
        End Get
        Set(ByVal value As Point)
            M_PStart = New Point(value.X, value.Y)
            SizeMe()
            Me.Refresh()
        End Set
    End Property
    Public Property EndPoint() As Point
        Get
            Return M_PStop
        End Get
        Set(ByVal value As Point)
            M_PStop = New Point(value.X, value.Y)
            SizeMe()
            Me.Refresh()
        End Set
    End Property
    Public Property BodyColor() As Color
        Get
            Return m_BodyColor
        End Get
        Set(ByVal value As Color)
            m_BodyColor = value
            Me.Refresh()
        End Set
    End Property
    Public Property IdentColor() As Color
        Get
            Return m_IdentColor
        End Get
        Set(ByVal value As Color)
            m_IdentColor = value
            Me.Refresh()
        End Set
    End Property
    Public Property BodyWidth() As Integer
        Get
            Return m_BodyWidth
        End Get
        Set(ByVal value As Integer)
            m_BodyWidth = value
            Me.Refresh()
            SizeMe()
        End Set
    End Property
    Public Property IdentWidth() As Integer
        Get
            Return m_IdentWidth
        End Get
        Set(ByVal value As Integer)
            m_IdentWidth = value
            Me.Refresh()
            SizeMe()
        End Set
    End Property
    Public Property LineStyle() As LineStyleConstants
        Get
            Return m_LineStyle
        End Get
        Set(ByVal value As LineStyleConstants)
            m_LineStyle = value
            Me.Refresh()
            SizeMe()
        End Set
    End Property
    Private Sub SizeMe()
        If M_PStart.X > 0 And M_PStart.Y > 0 And M_PStop.X > 0 And M_PStop.Y > 0 Then
            If M_PStart.X < M_PStop.X Then
                Me.Width = (M_PStop.X - M_PStart.X) + (m_BodyWidth * 4)
                Me.Left = M_PStart.X - (m_BodyWidth * 2)
            Else
                Me.Width = (M_PStart.X - M_PStop.X) + (m_BodyWidth * 4)
                Me.Left = M_PStop.X - (m_BodyWidth * 2)
            End If
            If M_PStart.Y < M_PStop.Y Then
                Me.Height = (M_PStop.Y - M_PStart.Y) + (m_BodyWidth * 4)
                Me.Top = M_PStart.Y - (m_BodyWidth * 2)
            Else
                Me.Height = (M_PStart.Y - M_PStop.Y) + (m_BodyWidth * 4)
                Me.Top = M_PStop.Y - (m_BodyWidth * 2)
            End If
        End If
        'Take a snapshot of the screen in the region behind the control 
        If Me.Height > 1 And Me.Width > 1 Then
            Dim bmp = New Bitmap(Me.Width, Me.Height)
            Dim MyGraphics = Graphics.FromImage(bmp)
            MyGraphics.CopyFromScreen(Me.Left, Me.Top, 0, 0, bmp.Size)
            MyGraphics.Dispose()
            Me.BackgroundImage = bmp
        End If
    End Sub
    Private Sub LabelLine_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'Me.Visible = False
        Dim G As Graphics = e.Graphics
        'G.Clear(Color.Transparent)
        G.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
        G.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        Dim X1 As Integer = M_PStart.X - Me.Left
        Dim X2 As Integer = M_PStop.X - Me.Left
        Dim Y1 As Integer = M_PStart.Y - Me.Top
        Dim Y2 As Integer = M_PStop.Y - Me.Top

        If m_LineStyle = LineStyleConstants.Solid Then
            Dim Pen1 As New Pen(m_BodyColor, m_BodyWidth)
            G.DrawLine(Pen1, X1, Y1, X2, Y2)
            Dim Pen2 As New Pen(m_IdentColor, m_IdentWidth)
            'G.DrawLine(Pen2, X1, Y1, X2, Y2)
        End If
        If m_LineStyle = LineStyleConstants.Line Then
            Dim Pen1 As New Pen(m_BodyColor, m_BodyWidth)
            G.DrawLine(Pen1, X1, Y1, X2, Y2)
            Dim Pen2 As New Pen(m_IdentColor, m_IdentWidth)
            G.DrawLine(Pen2, X1, Y1, X2, Y2)
        End If

        If m_LineStyle = LineStyleConstants.Circle Then
            Dim IdentBmp As New Bitmap(m_BodyWidth, m_BodyWidth, Imaging.PixelFormat.Format24bppRgb)
            Dim Ig As Graphics = Graphics.FromImage(IdentBmp)
            Ig.Clear(m_BodyColor)
            Dim Cx As Integer = (m_BodyWidth / 2) - m_IdentWidth / 2
            Dim Cy As Integer = Cx
            Dim Cw As Integer = m_IdentWidth / 2
            Dim Ch As Integer = Cw
            Ig.FillEllipse(New SolidBrush(m_IdentColor), Cx, Cy, Cw, Ch)
            Dim myBrush As New TextureBrush(IdentBmp)
            Dim Pen1 As New Pen(myBrush, m_BodyWidth)
            G.DrawLine(Pen1, X1, Y1, X2, Y2)
        End If

        If m_LineStyle = LineStyleConstants.Square Then
            Dim IdentBmp As New Bitmap(m_BodyWidth, m_BodyWidth, Imaging.PixelFormat.Format24bppRgb)
            Dim Ig As Graphics = Graphics.FromImage(IdentBmp)
            Ig.Clear(m_BodyColor)
            Dim Cx As Integer = (m_BodyWidth / 2) - m_IdentWidth / 2
            Dim Cy As Integer = Cx
            Dim Cw As Integer = m_IdentWidth / 2
            Dim Ch As Integer = Cw
            Ig.FillRectangle(New SolidBrush(m_IdentColor), Cx, Cy, Cw, Ch)
            Dim myBrush As New TextureBrush(IdentBmp)
            Dim Pen1 As New Pen(myBrush, m_BodyWidth)
            G.DrawLine(Pen1, X1, Y1, X2, Y2)
        End If

        If m_LineStyle = LineStyleConstants.Hatch Then
            Dim myBrush As New System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Plaid, m_BodyColor, m_IdentColor)
            Dim Pen1 As New Pen(myBrush, m_BodyWidth)
            G.DrawLine(Pen1, X1, Y1, X2, Y2)
        End If
    End Sub

    Private Sub LabelLine_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SizeMe()
        Refresh()
    End Sub

    Private Sub InitializeComponent()

    End Sub

    Private Sub LabelLine_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        If Me.Visible = True Then Me.Refresh()
    End Sub
    
End Class
